/*
package org.jeecg.modules.demo.com.kunpeng.utils;

import com.spire.pdf.PdfDocument;

import javax.print.PrintService;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;

public class FilePrintUtils {

    public static void PDFPrint(File file, String printerName, int width, int height, int marginLeft, int marginRight) {
        //判断file是否是一个pdf文件
        if (!isPdfFile(file)) {
            return;
        }

        //实例化PdfDocument类的对象
        PdfDocument pdf = new PdfDocument();
        //加载PDF文档
        pdf.loadFromFile(file.getAbsolutePath());
        //生成打印任务
        PrinterJob printJob = PrinterJob.getPrinterJob();
        //设置打印任务名称
        printJob.setJobName(file.getName());
        //查找名称打印机
        PrintService printService = getPrintService(printerName);

        if (printService != null) {
            try {
                printJob.setPrintService(printService);
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        } else {
            System.out.print("打印失败，未找到名称为" + printerName + "的打印机，请检查。");
            return;
        }

        //设置打印参数
        PageFormat pageFormat = printJob.defaultPage();
        Paper paper = getPaper(width, height, marginLeft, marginRight);
        pageFormat.setPaper(paper);
        printJob.setPrintable(pdf, pageFormat);
        //设置打印份数
        printJob.setCopies(1);
        try {
            printJob.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }

    //判断file是否是一个pdf文件
    private static boolean isPdfFile(File file) {
        if (!file.isFile()) {
            //非文件处理
            System.out.println("上送文件（" + file.getAbsolutePath() + "）不是一个文件");
            return false;
        }
        if (!file.getName().contains(".pdf")) {
            //非pdf文件处理
            System.out.println("文件（" + file.getName() + "）不是一个pdf文件");
            return false;
        }
        return true;
    }

    */
/**
     * 设置打印区域
     * @param width       宽度
     * @param height      高度
     * @param marginLeft  左边距
     * @param marginRight 右边距
     * @return
     *//*


    private static Paper getPaper(int width, int height, int marginLeft, int marginRight) {
        Paper paper = new Paper();
        int marginTop = 0;
        int marginBottom = 0;
        paper.setSize(width, height);
        // 下面一行代码，解决了打印内容为空的问题
        paper.setImageableArea(marginLeft, marginTop, width - (marginLeft + marginRight), height - (marginTop + marginBottom));
        return paper;
    }

    //查找名称打印机
    private static PrintService getPrintService(String printerName) {
        //获得本台电脑连接的所有打印机
        PrintService[] printServices = PrinterJob.lookupPrintServices();
        if (printServices == null || printServices.length == 0) {
            System.out.print("打印失败，未找到可用打印机，请检查。");
            return null;
        }
        //匹配指定打印机
        for (int i = 0; i < printServices.length; i++) {
            if (printServices[i].getName().contains(printerName)) {
                return printServices[i];
            }
        }
        return null;
    }
}
*/
